package ink.sunflowerk.handler;

import ink.sunflowerk.entity.MyDirectory;
import ink.sunflowerk.entity.MyFile;
import ink.sunflowerk.entity.ResponseVO;
import ink.sunflowerk.service.MyDirectoryService;
import ink.sunflowerk.service.MyFileService;
import ink.sunflowerk.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @author zhaokuii11@163.com
 * @create 2021-12-16 9:32
 * @Description
 */
@RestController
@RequestMapping("/myFile")
public class MyFileAction {
    @Autowired
    private MyDirectoryService myDirectoryService;
    @Autowired
    private MyFileService myFileService;
    @Autowired
    private UserService userService;

    //1 文件上传
    @RequestMapping(value = "/upload.action", method = RequestMethod.POST)
    public ResponseVO<String> uploadMethod(MyDirectory myDirectory, @RequestParam("dfile") MultipartFile file, HttpServletRequest req) throws Exception {
        //由文件获取md5
        String fileMd5 = DigestUtils.md5DigestAsHex(file.getBytes());
        //查询数据库  如果数据库中存在此md5  说明相同内容的文件已经存在
        MyFile myFile = myFileService.getOneByFmd5(fileMd5);
        if (myFile == null) {
            //上传
            String fileName = file.getOriginalFilename();
            String path = req.getSession().getServletContext().getRealPath("/imgs");
            File muDi = new File(path, System.currentTimeMillis() + "_" + fileName);
            file.transferTo(muDi);
            myFile = new MyFile();
            myFile.setFmd5(fileMd5);
            myFile.setFsize((int) (file.getSize() / 1024));//数据库存储的文件大小以kb为单位
            myFile.setFurl(muDi.getName());
            myFileService.addOne(myFile);
        }
        myFile = myFileService.getOneByFmd5(fileMd5);
        //创建mydirectory
        myDirectory.setDtype(1);//文件类型
        myDirectory.setDtime(new Date());
        myDirectory.setDsize(myFile.getFsize());
        myDirectory.setDprivate(0);
        myDirectory.setDfid(myFile.getFid());
        myDirectoryService.addOne(myDirectory);
        return new ResponseVO<String>(200, "文件上传成功！", null);
    }

    //2 文件下载
    @RequestMapping(value = "/download.action", method = RequestMethod.GET)
    public ResponseEntity<byte[]> downloadMethod(int fid, HttpServletRequest req) throws Exception {
        MyFile myFile = myFileService.getOneByFid(fid);
        //获取文件的类型
        String type = req.getSession().getServletContext().getMimeType(myFile.getFurl());
        //获取真实路径
        String realPath = req.getSession().getServletContext().getRealPath("/imgs");
        File yuanFile = new File(realPath, myFile.getFurl());
        byte[] arr = FileUtils.readFileToByteArray(yuanFile);

        //创建heander对象  响应头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", type);
        headers.set("Content-Disposition", "attachment;filename=" + URLEncoder.encode(myFile.getFurl(), "utf-8"));
        //获取源文件
        //context.get
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(arr, headers, HttpStatus.OK);
        return entity;
    }
}
